import {createRouter, createWebHistory} from "vue-router";
import {ElMessage} from 'element-plus';
//createWebHistory路由模式路径不带#号,生产环境下不能直接访问项目，需要nginx转发
//createWebHashHistory路由模式路径带#号

const router = createRouter({
    history: createWebHistory(),//history: createWebHashHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: "/",
            name: "Layout", //框架
            component: () => import("../layout/Layout.vue"),
            redirect: "/login",
            children: [
                {
                    path: "/users",
                    name: "UserManage",
                    component: () => import('@/views/userManage.vue')  //用户管理
                },
                {
                    path: '/files',
                    name: "FileManage",
                    component: () => import('@/views/fileManage.vue')  //文件管理
                },
                {
                    path: '/roles',
                    name: "RolesManage",
                    component: () => import('@/views/rolesManage.vue')  //权限管理
                },
                {
                    path: '/industryClassificationManage',
                    name: "IndustryClassificationManage",
                    component: () => import('@/views/industryClassificationManage.vue')  //项目类型分类
                },
                {
                    path: '/domainClassificationManage',
                    name: "DomainClassificationManage",
                    component: () => import('@/views/domainClassificationManage.vue')  //业务类型分类
                },
                {
                    path: '/index',
                    name: "Index",
                    component: () => import('@/views/index.vue')  //首页
                },
                {
                    path: '/myFile',
                    name: "MyFile",
                    component: () => import('@/views/myFile.vue')  //我的文件
                },
                {
                    path: '/specificationLibrary',
                    name: "SpecificationLibrary",
                    component: () => import('@/views/specificationLibrary.vue')  //规范库
                },
                {
                    path: '/inviteTendersFileAnalysis',
                    name: "InviteTendersFileAnalysis",
                    component: () => import('@/views/inviteTendersFileAnalysis.vue')  //招标文件分析
                },
                {
                    path: '/fileMatchingAnalysisOfInviteTenders',
                    name: "FileMatchingAnalysisOfInviteTenders",
                    component: () => import('@/views/fileMatchingAnalysisOfInviteTenders.vue')  //招投标文件匹配分析
                },
                {
                    path: '/automaticBidEvaluation',
                    name: "AutomaticBidEvaluation",
                    component: () => import('@/views/automaticBidEvaluation.vue')  //自动评标
                },
                {
                    path: '/inviteTendersFileSpecification',
                    name: "InviteTendersFileSpecification",
                    component: () => import('@/views/inviteTendersFileSpecification.vue')  //招标文件规范
                },
                {
                    path: "/setting",
                    name: "Setting",
                    component: () => import("@/views/setting.vue"),//设置
                },
                {
                    path: "/shares",
                    name: "Share",
                    component: () => import("@/views/shareManage.vue"),//分享管理
                },
                {
                    path: "/myShare",
                    name: "MyShare",
                    component: () => import("@/views/myShare.vue"),//我的分享
                },
                {
                    path: "/trashManage",
                    name: "TrashManage",
                    component: () => import("@/views/trashManage.vue"),//回收站，含管理员和用户的
                },

                {
                    path: "/notice",
                    name: "Notice",
                    component: () => import("@/views/systemNotice.vue"),//系统公告
                },
                {
                    path: "/noticeManage",
                    name: "NoticeManage",
                    component: () => import("@/views/systemNoticeManage.vue"),//公告管理
                },
                {
                    path: '/view',
                    name: "ViewSpecifications",
                    component: () => import('@/views/viewSpecifications.vue') //规范查看
                },
                {
                    path: '/detail',
                    name: "SpeDetail",
                    component: () => import('@/views/SpeDetail.vue') //规范查看
                },
                {
                    path: '/test',
                    name: "Test",
                    component: () => import('@/views/test.vue')  //测试页面
                },
                {
                    path: '/assistant',
                    name: "SystemAssistant",
                    component: () => import('@/views/systemAssistant.vue')  //系统助手
                },
                {
                    path: '/conversation',
                    name: "Conversation",
                    component: () => import('@/views/conversationManage.vue')  //会话管理
                },

                {
                    path: '/conversionDetail',
                    name: "ConversionDetail",
                    component: () => import('@/views/conversionDetail.vue')  //会话详情
                },
                {
                    path: '/assistantManage',
                    name: "AssistantManage",
                    component: () => import('@/views/assistantManage.vue')  //助手管理
                },
                {
                    path: '/problemFeedback',
                    name: "ProblemFeedback",
                    component: () => import('@/views/problemFeedback.vue')  //问题反馈
                },
                {
                    path: '/reportSheet',
                    name: 'ReportSheet',
                    component: () => import ('@/views/reportSheet.vue')//报告单管理
                },
                {
                    path: '/myReportSheet',
                    name: 'MyReportSheet',
                    component: () => import ('@/views/myReportSheet.vue')//我的报告单
                },
            ],
        },
        {
            path: "/login",
            name: "Login",
            component: () => import("@/views/login.vue"),
        },
        {
            path: "/register",
            name: "Register",
            component: () => import("@/views/register.vue"),
        },
        {
            path: "/findPassword",
            name: "FindPassword",
            component: () => import("@/views/passwordRecovery.vue"),
        },
        {
            path: '/showDoc',
            name: 'ShowDoc',
            component: () => import ('@/views/DocView.vue')
        },
        {
            path: '/downShare',
            name: 'DownShare',
            component: () => import ('@/views/downShare.vue')
        },
        {
            path: '/header',
            name: 'HeaderView',
            component: () => import ('@/components/HeaderView.vue')
        },
    ],
});

//登录拦截
// 配置路由守卫  三个可选参数，to去往的目标，from来源的目标对象，next接下来的操作
router.beforeEach((to, from, next) => {
    console.log("现在所在客户端的路由：", to.path)
    const token = localStorage.getItem('token')
    console.log("token:", token)
    // 如果访问的是登录和注册页面，直接放行   再放行弹窗访问的showDoc页面
    if (to.path === '/login') {
        if (!token) {
            return next() //无token（没有登录状态）就能到login页面
        } else {
            return next('/index')  //有token（有登录状态）就不能到login页面
        }
    }

    if(to.path === '/register' || to.path ===  '/showDoc' || to.path === '/findPassword'){
        return next() //直接可以到
    }


    // 如果用户未登录，并且访问的不是登录页面

    // console.log("路由守卫输出的token：", token)
    if (!token) {
        ElMessage.info("客户端提示：未登录或已过期,请登录！")
        return next('/login')// 如果没有token，则跳转到登录页面
    }

    if (JSON.parse(localStorage.getItem('userRoles')).some(role => role === 'Admin') &&
        (      to.path === '/myFile'
            || to.path === '/industryClassificationManage'
            || to.path === '/domainClassificationManage'
            || to.path === '/specificationLibrary'
            || to.path === '/inviteTendersFileAnalysis'
            || to.path === '/fileMatchingAnalysisOfInviteTenders'
            || to.path === '/automaticBidEvaluation'
            || to.path === '/inviteTendersFileSpecification')) {
        ElMessage.info("您无权访问" + to.path + "页面！")
        return next('/index')
    }

    if (JSON.parse(localStorage.getItem('userRoles')).some(role => role === 'GfkAdmin') &&
        (      to.path === '/users'
            || to.path === '/roles'
            || to.path === '/noticeManage'
            || to.path === '/assistantManage'
            || to.path === '/conversation'
            || to.path === '/files'
            || to.path === '/shares'
            || to.path === '/problemFeedback')) {
        ElMessage.info("您无权访问" + to.path + "页面！")
        return next('/index')
    }

    // 如果已经登陆，则直接放行
    next();
})

export default router;
